package initzation

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"log"
	"serviceApi/global"
	"serviceApi/proto/GoodSrv"
	"serviceApi/proto/UserSrv"
	"serviceApi/router"
)

func init() {
	//初始化日志文件
	InitZap()
	//初始化支付宝支付
	InitAliPay()
	//初始化用户服务端的链接
	InitUserSrv()
	//初始化商品服务端的连接
	InitGoodSrv()
	//初始化路由设置
	InitRouter()
}

func InitZap() {
	config := zap.NewDevelopmentConfig()
	config.OutputPaths = []string{
		"D:\\GO\\src\\week3\\serviceApi\\zap.log",
	}
	build, err := config.Build()
	if err != nil {
		return
	}
	zap.ReplaceGlobals(build)
}

func InitAliPay() {
	var err error

	if global.AliPayClient, err = alipay.New("9021000138676027", "MIIEowIBAAKCAQEAhFOb8NdJYSE1PH7vJxhHq2cfSywUuBUKPRjgkGqGi6nCSu9IuP8j9UEGUUT7W2y1LXEe4e1GmMnu5N8d5XaavmXf3yuOoYNcxRPlN5m3d58UuXBtfYGQOy96Zfk9+Dt/p9Zvc1A0VV7PPcDlxt/TpiKYDgJ9mRwmRWlcqp+VGYMaWjKkSrQ3WsQHkn9gP4RLUu2OB8u8MMB36gzTrMp/9qWBqY1ZfqKEw/34C2cSUxxf5O/PDkL2yy/qF5yulaak+FzHzvjtwIj+yKDjuQiS/rw0KAqbh3lmmv8Y7QWprWXdnMn72LTa3Or53mUGcCEbVZCM00L0jg3qRLPio+mX1wIDAQABAoIBABFwZ2PFK6GGW9q+K8xuMbvQGlqw04YzF6dXqPi/YT30wGtZv67JyTZgPHHncmzpFbOIbfZ+a8hlDHvTP8Bm8hvXlzp1JhX5FvfOGG5MbtZv+644t+VLdvOl3S6lO1qElN1MsGF1F4CMvyk3S9D63IzPkFSAVYchI5YQPasq3t5+rS4fiFxt4N6dO9sQ/dHd0sWVzVl/+s+/rRfxCguWl0KBY0KOI1NxPejhSWQ7dqeU1YvJUgVh6fM8Ny594a/ORjTObiyyIGLoeo3HnNc+fJBaQawjAOjMqnYZU5iny66ppKXR26zgz2ER0S5NwJOhUo4SppS8VnrcNF43uJTZv4ECgYEAuBhdN8/mSZ4OaJ+FbOdfbaFLbgZqU5L7Tuo91IqEOQR5JG1SVPu/WKmNNPBb96tx0eZKLV+ShQe+RkzMzCZmwMOc9IG2XG3MQxXPfft0vIz6rR9VRVcy13U1bH74F35T6n9D4PkPTGRNLGoYJHtLyce3xM61xck7CAfEvIeR+hcCgYEAuALqqUW2U5C4gv1ib0Xlehd1hNTaAReTZj8lKQawxyLDebiO5vFtbpyVY5cmZ+UvKO8Pc6kPQKziDTroiiWBsRMl/qlIdq0GQZMJ/cSXqrrxN6WK80GgqCQaxwR2yGG3EIEg7eNWWd0mMrNgRUxNvXYZYIDz4O204aH5WdhWqEECgYBXPiSfGIVYCfFmH4MJszSELdYjVCL7bEJwiY23tdUneDk4zB4ibBOoHuHuXqIW40bTZEFu80XqiVrg0fCUJva3D3zIWthjQR12BVYJrCsMV/tMI/czHznW5ZLl0d4ctB9sl9dBPIbFexsfmz/JuiyWeXFbcWKvUsgI7DWN6umBDQKBgQCuxh5qTothjbiiudZPx/yw3S1C0ASMOv9Ggz1hXTh0u+EccJ5SXgdJozIFGKBi1/zWDVII0g9pp4tBc61A4WTwcjaJTfJEvgyOJgG8ZYiEofDQoMx8Rd45Nam1U9kadC0zYOKM8Huo+PL5/wO2bkX8aPqrfW+mOUCvem/i1SdhwQKBgHHf39CiV29JH4/AM3cVswyzgT/cHokb03r9NWbzgdJMD51ctmTsm4CQ0EHtb0tQSoTQSAVvY5WUJFiHarjp/lBqn6qf9KBDahAoiblMy5fnnSzEnDn47HkekGa+Q6IJ9wz/+uXQkYHu4fujJnsYNJ3HK9slXbFXnnJMjDJtgS3e", false); err != nil {
		log.Println("初始化支付宝失败", err)
		return
	}
	global.AliPayClient.LoadAliPayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk7xNtPRLppAnWKJs4fnG//t1BnzG8IabeWwG9KY3veUlcIWs/mTuezZT2bLeRRe/S5tpUQl3+piOb0HsG9bhCuW+OBwJyiFJZItkELnAFt8oSkZ27HhWfDbdHARkjk6iWkfPTa4UF+tk+B1QSPlQGvY2bhxgKNYM2Uf/IYx7uWAssF/kvb6Z2V30CDoOvGWloLojRljwm54bAeum6hp7/6QSqQPEWaAywabRLIti4D/HPc8Hkx12DzcwV0sl3tgRJ7IqwELdW52GylRGrC74z7AN/Unfg9dpPhl/wQSIlyTT/970gyUeqCPe+WeB2dOfidc81V0i4ZNSjrmdg7KnywIDAQAB")

}

func InitGoodSrv() {
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8889", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	// 2. 实例化gRPC客户端
	global.GoodClient = GoodSrv.NewGoodClient(conn)

}

func InitUserSrv() {
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	// 2. 实例化gRPC客户端
	global.UserClient = UserSrv.NewUserClient(conn)
}

func InitRouter() {
	r := gin.Default()
	router.Router(r)
	global.Router = r
}
